PreviousNextTracker indexSee it online !

(176/185) 387 - Git-Plugin show branch in tree

I would like to see in Project Viewer the currently checked out git branch.

I'm not sure whether a change in git-plugin is sufficient or a change in Project viewer is required.

My idea would be to display the branch (instead of or) additionally to the project name in the project viewer tree. So if the project is called "foo" and the branch is "bar" the root of the tree should show "foo (bar)".

I couldn't yet find in the source how the name is displayed let alone how I could change this.

Anybody here who could do this change or guide me?

Submitted ngc - 2017-11-15 16:14:28.775000 Assigned
Priority 5 Labels git-plugin
Status open Group None
Resolution None

Comments

2017-11-19 18:24:17.661000
ngc

I've created a macro that helps (me) remedy the situation a bit:

import git.command.BranchList;
import projectviewer.vpt.VPTProject;
import projectviewer.ProjectViewer;
import projectviewer.vpt.VPTNode;
import projectviewer.ProjectManager;

// Do we have Projct Plugin?
EditPlugin ep = jEdit.getPlugin("projectviewer.ProjectPlugin", false);
if (ep == null) {
return;
}
ProjectViewer pv = ProjectViewer.getViewer(view);
VPTNode node= ProjectViewer.getActiveProject(view);

// Retrieve all branches
BranchList bl= new BranchList(node.getRootPath());
if (!bl.execute(pv.getView())) {
return;
}

// get current name
String currentName= node.getName();
// pull out the branch
String currentBranch= currentName.replaceAll("^.*? \\[", "");

// get the current branch
String newBranch= bl.getCurrent() + "]";

// same as the one we already have -> leave
if (newBranch == currentBranch) {
return;
}

// replace previous branch with current branch
String newName= currentName.replaceAll(" \\[.*$", "") + " [" + newBranch;

node.setName(newName);
ProjectManager.getInstance().renameProject(currentName, newName);
pv.nodeChanged(node);
node.firePropertiesChanged();

pv.repaint();

When I call this macro, the current project's name is changed so that the current branch in square brackets is appended.

Do we have experts here which can enhance ProjectViewer and/or GitPlugin to do this automatically upon each (re-)import or opening of a project?

2017-11-19 21:50:13.005000
daleanson

You could add essentially the same code to GitPlugin.java, like this:
~~~
package git;

import git.command.BranchList;

import org.gjt.sp.jedit.EBMessage;
import org.gjt.sp.jedit.EBPlugin;
import org.gjt.sp.jedit.EditPlugin;
import org.gjt.sp.jedit.jEdit;
import org.gjt.sp.jedit.View;

import projectviewer.ProjectManager;
import projectviewer.ProjectViewer;
import projectviewer.event.ViewerUpdate;
import projectviewer.vpt.VPTProject;

/** Plugin for the git version control system. */
public class GitPlugin extends EBPlugin {

public static String gitPath() {
return jEdit.getProperty( "git.path", "git" );
}

public void handleMessage( EBMessage message ) {
if ( message instanceof ViewerUpdate ) {
ViewerUpdate msg = (ViewerUpdate)message;
if (msg.getType() != ViewerUpdate.Type.PROJECT_LOADED)
return;

// Do we have Projct Plugin?
EditPlugin ep = jEdit.getPlugin( "projectviewer.ProjectPlugin", false );
if ( ep == null ) {
return; // shouldn't happen, PV is a dependency for this plugin
}

View view = jEdit.getActiveView();
ProjectViewer pv = ProjectViewer.getViewer( view );
VPTProject node = ProjectViewer.getActiveProject( view );

// Retrieve all branches
BranchList bl = new BranchList( node.getRootPath() );
if ( !bl.execute( pv.getView() ) ) {
return;
}

// get current name
String currentName = node.getName();

// pull out the branch
String currentBranch = currentName.replaceAll( "^.*? \\[", "" );

// get the current branch
String newBranch = bl.getCurrent() + "]";

// same as the one we already have -> leave
if ( newBranch.equals(currentBranch) ) {
return;
}

// replace previous branch with current branch
String newName = currentName.replaceAll( " \\[.*$", "" ) + " [" + newBranch;

// rename the project to include the branch name
node.setName( newName );
ProjectManager.getInstance().renameProject( currentName, newName );
pv.nodeChanged( node );
node.firePropertiesChanged();

pv.repaint();
}
}
}

~~~

2017-11-19 21:56:38.653000
daleanson

However, I'm not sure the plugin should just automatically change the name of the project without the users saying so. Probably the GitPlugin should provide an option pane for ProjectViewer for the user to be able to choose this action.

2017-11-23 07:26:54.610000
ngc

If I'm not mistaken you already added it, right?

Unfortunately I get issues having the rename active. You cannot reimport a project and the troubleshoot utility shows exceptions.

08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: Exception in thread "AWT-EventQueue-0"
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: java.lang.NullPointerException
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at projectviewer.ProjectManager.saveProject(ProjectManager.java:282)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at projectviewer.gui.ImportDialog.ok(ImportDialog.java:319)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at common.gui.OkCancelButtons.actionPerformed(OkCancelButtons.java:92)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.Component.processMouseEvent(Component.java:6533)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.Component.processEvent(Component.java:6298)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.Container.processEvent(Container.java:2237)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.Component.dispatchEventImpl(Component.java:4889)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.Container.dispatchEventImpl(Container.java:2295)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.Component.dispatchEvent(Component.java:4711)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4526)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.Container.dispatchEventImpl(Container.java:2281)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.Window.dispatchEventImpl(Window.java:2746)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.Component.dispatchEvent(Component.java:4711)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventQueue.access$500(EventQueue.java:97)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventQueue$3.run(EventQueue.java:709)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventQueue$3.run(EventQueue.java:703)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.security.AccessController.doPrivileged(Native Method)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventQueue$4.run(EventQueue.java:731)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventQueue$4.run(EventQueue.java:729)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.security.AccessController.doPrivileged(Native Method)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:109)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:190)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:235)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:233)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.security.AccessController.doPrivileged(Native Method)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:233)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.Dialog.show(Dialog.java:1084)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.Component.show(Component.java:1671)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.Component.setVisible(Component.java:1623)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.Window.setVisible(Window.java:1014)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.Dialog.setVisible(Dialog.java:1005)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at projectviewer.gui.ImportDialog.internalShow(ImportDialog.java:558)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at projectviewer.gui.ImportDialog.access$000(ImportDialog.java:90)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at projectviewer.gui.ImportDialog$1.run(ImportDialog.java:501)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at projectviewer.PVActions.swingInvoke(PVActions.java:507)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at projectviewer.gui.ImportDialog.setVisible(ImportDialog.java:498)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at projectviewer.importer.FileImporter$DialogRunner.run(FileImporter.java:192)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:301)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventQueue.access$500(EventQueue.java:97)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventQueue$3.run(EventQueue.java:709)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventQueue$3.run(EventQueue.java:703)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.security.AccessController.doPrivileged(Native Method)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
08:25:30 [AWT-EventQueue-0] [error] AWT-EventQueue-0: at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
08:25:31 [AWT-EventQueue-0] [debug] EditBus: ViewUpdate[what=VIEW_ACTIVATED,source=org.gjt.sp.jedit.View[active]]
08:25:31 [AWT-EventQueue-0] [debug] jEdit: checkBufferStatus for all buffers
08:25:31 [AWT-EventQueue-0] [debug] EditBus: DynamicMenuChanged[menu=recent-files,source=null]
08:25:31 [AWT-EventQueue-0] [debug] EditBus: DynamicMenuChanged[menu=recent-files,source=null]
08:25:31 [jEdit Worker #3] [debug] GitSubmoduleImporter: Running git ls-files in /Users/skeeve/Library/jEdit/jars/GitPlugin
08:25:32 [jEdit Worker #3] [error] ReImporter: java.lang.NullPointerException
08:25:32 [jEdit Worker #3] [error] ReImporter: at projectviewer.ProjectManager.saveProject(ProjectManager.java:282)
08:25:32 [jEdit Worker #3] [error] ReImporter: at projectviewer.ProjectManager.saveProject(ProjectManager.java:268)
08:25:32 [jEdit Worker #3] [error] ReImporter: at projectviewer.importer.Importer._run(Importer.java:563)
08:25:32 [jEdit Worker #3] [error] ReImporter: at org.gjt.sp.util.Task.run(Task.java:65)
08:25:32 [jEdit Worker #3] [error] ReImporter: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
08:25:32 [jEdit Worker #3] [error] ReImporter: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
08:25:32 [jEdit Worker #3] [error] ReImporter: at java.lang.Thread.run(Thread.java:748)

2017-11-23 07:43:36.674000
ngc

Update: There must be a fundamental difference between what you did and what my Macro does.

I think it could be due to that my macro renames the project and you test whether or not to rename it by checking

GitPVOptionPane.INTERNAL_NAME + '.' + projectName + ".autoRename"

But if you renamed it, the name changed. Could it be there is a mismatch between the name and the INTERNAL_NAME? Or that there is none and so the setting is still stored under the old name and so you cannot find it?

Maybe it's better to have a global GIT setting to rename every GIT project to include the branch?

2017-11-23 13:48:01.768000
ngc

I've tested by deactivating the storing of the property and the exception is gone.

So we either need to have the autorename be a global GIT option (I'd vote for that).

Or find a way to store it per project without having to deal with the problem that the property contains the project's name.

I also tried it by changing the "getProjectName" method in GitPVOptopnPane to

private String getProjectName() {
VPTProject project = ProjectViewer.getActiveProject( jEdit.getActiveView() );
return project == null ? "" : project.getName().replaceAll(" \\[.*", "");
}

But other functionality then fails e.g. commiting. But it might fail anyway because the project name contains spaces and brackets.

2017-11-23 14:44:59.969000
daleanson

I just pushed a change, would you give it a try? The change makes sure the rename happens on the EDT, which appears to fix the problem. I say "appears" because I rebuilt ProjectViewer to help with debugging this, so my version of ProjectViewer may not be the same as the one from the plugin manager. I don't think that should make a difference, but let me know if this change doesn't work for you.

2017-11-23 22:08:05.579000
ngc

Seems to work except for the creation of a project.

I created a project and choose my GitPlugin repo. I selected the new option but the branch name was not added.

I went to the fresh roject's properties and the option (Automatically add branch name to project name) was unchecked.

Upon setting the checkbox everything seems to work. I even added the german translation for the option and submitted a merge request for it.

2017-11-28 14:23:17.331000
ngc

How will the branch be updated? Re-import of files didn't do that.

2017-11-28 16:20:25.824000
daleanson

I'll check, it updates when ProjectViewer sends a message on the EditBus, but it might not be checking for all the right messages, like on reimport.

2017-11-29 10:23:10.941000
ngc

FYI: Plugins > Git Plugin > Checkout Branch does not send a message on the EditBus, so the branch isn't updated in the view.

2017-11-29 10:24:38.174000
ngc

Additional Info: After checking out another branch, the setting about automatically adding the branch name is lost.

Update: Seems it's a per-branch setting ;)

I had the setting switch on on "master"
I switched to "staging" -> Setting lost
I swiched it on on "staging" and checked out "master"
Setting was there.